<?php
///////
// webirc-client - 2013-2020
// (C) Chris Dorman, GPL v3
// https://notabug.org/Pentium44/ircchat
///////

// irc.php - used to push and pull data from IRC server.
// Currently supports PING / PONG, data receive, and data push
// Done via PHP sockets.

// Prevent PHP from stopping the script after 30 sec
set_time_limit(0);

// Include variables
include_once("config.php");

// Get username from command line argument / PHP-CLI
$username = $argv[1];

// If username isn't set, exit with error.
if(!isset($username) || $username == "") {
	echo "Username not given...";
	exit(1);
}

// Create a socket to use
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

// Connect to IRC server via socket above
socket_connect($socket, $server, $port);

// NICK and USER calls to IRC 
$nickline = "NICK " . $username . "\n";
$userline = "USER " . $username . " 0 * :" . $username . "'s Bot\n";

// Pass NICK and USER back to IRC server over socket
socket_write($socket, $nickline, strlen($nickline));
socket_write($socket, $userline, strlen($userline));

sleep(1);

// Continue the rest of the script here
// While script will continue as long as socket continues to be active
while($bytes = socket_recv($socket, $r_data, 2048, MSG_DONTWAIT) !== '') {
	if($bytes !== FALSE) {
		//$data = socket_read($socket, 2048, PHP_NORMAL_READ);
		$data = $r_data;
	}

	// If client sent something, push it to the IRC server!
	if(file_exists(".$username.push")) {
		// Grab IRC command from server.php
		$pushFile = file_get_contents(".$username.push");
		// Push this / these commands to socket
		socket_write($socket, $pushFile, strlen($pushFile));
		// Remove the push file
		unlink(".$username.push");
	}
        
	// Check if web client still up, if no pong after 15 seconds, connection closed.
	if(!file_exists(".$username.pingfile")) { // If file is missing, quit
		// Debug logging, check if IRC is exiting properly
		doLog("Exiting, $username logged out...");
		$quitline = "QUIT :$username left the web client\n"; // IRC QUIT line
		socket_write($socket, $quitline, strlen($quitline)); // Push to socket
		socket_close($socket); // Close the socket
		exit(0); // Exit the script, nothing to do.
	} else if (date("YmdHis.", filemtime(".$username.pingfile"))<(date("YmdHis.", filemtime(".$username.pingfile"))-10)) {
		// Debug logging, check if IRC is exiting properly
		doLog("Exiting, $username timed out...");
		$quitline = "QUIT :$username's web session timed out\n"; // IRC QUIT line
		socket_write($socket, $quitline, strlen($quitline)); // Push to socket
		socket_close($socket); // Close the socket
		exit(1); // Exit the script, nothing to do.
	}
	
	// If data variable is set and buffer has data to recieve
	// RECIEVE IT!
	if(isset($data)) { // If data variable is set, there's data from socket
		$stringMsg = explode('PRIVMSG', $data); // Strip IRC commands
		// Get original contents from socket
		$socketFileContents = file_get_contents(".$username.socket");
		// Push all content to 
		file_put_contents(".$username.socket", $socketFileContents . $data);
		$ex = explode(' ', $data);
        
		// Send PONG back to the server
		if ($ex[0] == "PING") {
			// Log pong
			doLog("PONG, $username response...");
			$pongline = "PONG " . $ex[1] . "\n"; // PONG IRC CMD
			// Push to IRC server via socket.
			socket_write($socket, $pongline, strlen($pongline));
		}
	}
	
	// Half second sleep to prevent insane CPU load	
	usleep(500);
}

?>
